#include "graphics.h"

Game* Graphics::game_;

#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif

void Graphics::frame() {
    //All we do here is tell the universe to step once, then we swap the buffers
    //The universes drawable collection will take care of all the rendering
	glClear(GL_COLOR_BUFFER_BIT);
	
	reset();
    
	game_->step();
	
    glutSwapBuffers();
}

void Graphics::timercall(int value) {
    glutTimerFunc(SLEEP_TICKS, timercall, 0);	
    glutPostRedisplay();
}

void Graphics::initGL() {
    glClearColor(0.0, 0.0, 0.0, 0.0);

    glPointSize(3.0);

    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_POINT_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
	glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
    //Moving to camera.cpp as a test
	//glLineWidth(4.0f);

    glMatrixMode(GL_PROJECTION);
}

void Graphics::reset() {
	glLoadIdentity();
	//Move this to camera, have camera decide ortho
    
}

void Graphics::init(Game* g, int width, int height, int argc, const char *argv[]) {
    game_ = g;
    glutInit(&argc, (char**)argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(width, height);
    glutCreateWindow("Progrezion V2");
    initGL();
    glutDisplayFunc(frame);
    glutTimerFunc(SLEEP_TICKS, timercall, 0);

    //Input functions
    glutKeyboardFunc(game_->getInputClass().keyboardCb);
    glutKeyboardUpFunc(game_->getInputClass().keyboardUpCb);
    glutPassiveMotionFunc(game_->getInputClass().mouseCb);
	glutMouseFunc(game_->getInputClass().mouseClickCb);
}

